package com.mdxl.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chengjian on 2017/5/24.
 */
public class MdxlDateUtil {

    //获取当前时间的前num天数日期，以数组形式存储起来
    public static String[] gainDate(int num){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String[] week =new String[num];
        c.add(Calendar.DATE, 0);
        Date monday1 = c.getTime();
        String preMonday1 = sdf.format(monday1);
        week[0] =preMonday1;
        for (int i =1 ;i<num;i++) {
            c.add(Calendar.DATE, -1);
            Date monday = c.getTime();
            String preMonday = sdf.format(monday);
            week[i] =preMonday;
        }
        return week;
    }
    //判断两个日期时间相隔多少天，用这个版本，这个不是结果不是很严谨，但是符合我们常识
    public static int dayFakeNum(Date dateStart,Date dateEnd){

        Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(dateStart);

        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

        aCalendar.setTime(dateEnd);

        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

        return day2 - day1;
    }
    //严谨的结果不符合我们的业务逻辑
    public static int dayRealNum(Date dateStart,Date dateEnd){
        if (null == dateStart || null == dateEnd) {
            return -1;
        }
        long intervalMilli = dateEnd.getTime() - dateStart.getTime();
        return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }
    //获取当前月的前num个月
    public static int[] monthNum(int num){
        Calendar date=Calendar.getInstance();
        int month=date.get(Calendar.MONTH)+1;//获取当前日期的月份
        int[] a=new int[num];
        for (int i = 1; i <= a.length; i++) {
            if(month>6){
                //当前月份大于6月
                a[i-1]=(month-6+i);
            }else{
                //当前月份小于等于6月
                if(month-i<0){
                    a[a.length-i]=(month-i+13);
                }else{
                    a[a.length-i]=(month-i+1);
                }
            }
        }
        for (int i=0;i<num;i++){
            System.out.println(a[i]);
        }
        return a;
    }
}
